package com.cy.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.cy.condition.BackUserCondition;
import com.cy.entity.BackPurview;
import com.cy.entity.BackUser;
import com.cy.entity.NormalStatus;
import com.cy.util.Md5Util;
import com.cy.util.StringUtils;
/**
 * 后台用户DAO
 */
@Component("backUserDao")
public class BackUserDao extends BaseDAOHibernate<BackUser>{

	/**
	 *  Created on 2012-8-27 
	 * <p>Discription:[保存后台用户]</p>
	 * @param backUser
	 * @return BackUser
	 */
	public BackUser saveBackUser(BackUser backUser) {
		this.getHibernateTemplate().save(backUser);
		String sid =backUser.getId().toString();
		Date d = backUser.getCreateTime();
		SimpleDateFormat sdf =new SimpleDateFormat("yyMMdd");
		String dd = sdf.format(d);
		backUser.setBackuserNo( dd+org.apache.commons.lang.StringUtils.leftPad(sid, 4, "0"));
		this.getHibernateTemplate().update(backUser);
		return backUser;
		
	}
	
	/**
	 *  Created on 2012-8-27 
	 * <p>Discription:[根据登录帐号查询用户]</p>
	 * @param loginName
	 * @return BackUser
	 */
	public BackUser findBackUserByLoginName(String loginName) {
		if(StringUtils.isEmpty(loginName)){
			return null;
		}
		Session session=this.getSession();
		StringBuffer sb=new StringBuffer();
		sb.append(" FROM BackUser b WHERE b.loginName='"+loginName.trim()+"'");
		List<BackUser> list= new ArrayList<BackUser>();
		list=session.createQuery(sb.toString()).list();
		if(list.isEmpty()){
			return null;
		}
		return list.get(0);
		
	}
	
	public List<BackUser> listBackUserByCondition(int firstResult,int maxResults, BackUserCondition condition) {
		Session session=this.getSession();
		StringBuffer hql=new StringBuffer();
		List<BackUser> backUsers=new ArrayList<BackUser>();
		
		hql.append(" FROM  BackUser b WHERE 1=1 ");
		
		if(condition!=null){
			if(StringUtils.isNotEmpty(condition.getBackuserNo())){
				hql.append(" AND b.backuserNo=:backuserNo ");
			}
			if(StringUtils.isNotEmpty(condition.getNickname())){
				hql.append(" AND b.nickname like :nickname ");
			}
		}
		hql.append(" ORDER BY b.createTime DESC ");
		
		Query query = session.createQuery(hql.toString());
		
		if(condition!=null){
			if(StringUtils.isNotEmpty(condition.getBackuserNo())){
				query.setParameter("backuserNo", condition.getBackuserNo());
			}
			if(StringUtils.isNotEmpty(condition.getNickname())){
				query.setParameter("nickname", "%"+condition.getNickname()+"%");
			}
		}
		
		query.setFirstResult(firstResult);
		query.setMaxResults(maxResults);
		
		backUsers=query.list();
		return backUsers;
	}
	
	public int countBackUserByCondition(BackUserCondition condition) {
		Session session=this.getSession();
		StringBuffer hql=new StringBuffer();
		hql.append(" SELECT COUNT(*) FROM  BackUser b WHERE 1=1 ");
		if(condition!=null){
			if(StringUtils.isNotEmpty(condition.getBackuserNo())){
				hql.append(" AND b.backuserNo=:backuserNo ");
			}
			if(StringUtils.isNotEmpty(condition.getNickname())){
				hql.append(" AND b.nickname like :nickname ");
			}
		}
		Query query = session.createQuery(hql.toString());
		if(condition!=null){
			if(StringUtils.isNotEmpty(condition.getBackuserNo())){
				query.setParameter("backuserNo", condition.getBackuserNo());
			}
			if(StringUtils.isNotEmpty(condition.getNickname())){
				query.setParameter("nickname", "%"+condition.getNickname()+"%");
			}
		}
		return Integer.parseInt(query.uniqueResult()+"");
	}
	
	public BackUser findBackUserByLoginNameAndPassWord(String loginName,String password) {
		List<BackUser> backUsers=new ArrayList<BackUser>();
		String hql=" FROM BackUser bu WHERE bu.loginName=:loginName AND bu.password=:password ";
		backUsers=this.getSession().createQuery(hql).setParameter("loginName", loginName).setParameter("password", Md5Util.MD5Encode(password)).list();
		if(backUsers.isEmpty()){
			return null;
		}else{
			return backUsers.get(0);
		}
	}

	public List<BackPurview> listBackUserAllPurview(Long id) {
		List<BackPurview> backPurviews=new ArrayList<BackPurview>();
		Session session=this.getSession();
		StringBuffer hql=new StringBuffer();
		hql.append(" SELECT bp.purview FROM BackUserRole br,BackRolePurView bp ");
		hql.append(" WHERE br.role=bp.role AND br.backUser.id=:id AND br.backUser.status=:status AND bp.purview.status=:status ");
		hql.append(" ORDER BY bp.purview.sortTree asc ");
		Query query=session.createQuery(hql.toString());
		query.setParameter("id", id);
		query.setParameter("status", NormalStatus.NORMAL);
		List<BackPurview> purviews=query.list();
		if(!purviews.isEmpty()){
			for(BackPurview bp:purviews){
				if(!backPurviews.contains(bp)){
					backPurviews.add(bp);
				}
			}
		}
		return backPurviews;
		
	}
	
}
